<?php

/**
 * This is the model class for table "menu".
 *
 * The followings are the available columns in table 'menu':
 * @property integer $id
 * @property string $ad
 * @property string $baslik
 * @property integer $dil_id
 *
 * The followings are the available model relations:
 * @property MenuOge[] $menuOges
 */
class Menu extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Menu the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'menu';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ad, baslik, dil_id', 'required'),
			array('dil_id', 'numerical', 'integerOnly'=>true),
			array('ad, baslik', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, ad, baslik, dil_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    //'categories'=>array(self::MANY_MANY, 'Category','tbl_post_category(post_id, category_id)'),
			
                    'menuOges' => array(self::MANY_MANY, 'Oge', 'menu_oge(menu_id, oge_id)'),
                    'dil' => array(self::BELONGS_TO, 'Dil', 'dil_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'ad' => 'Ad',
			'baslik' => 'Baslik',
			'dil_id' => 'Dil',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ad',$this->ad,true);
		$criteria->compare('baslik',$this->baslik,true);
		$criteria->compare('dil_id',$this->dil_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function menuOgeleriGetir($menuAdi,$altOgeleriGetir) {
        $criteria = new CDbCriteria;
        $criteria->alias = 'm';
        $criteria->addCondition("m.ad = '{$menuAdi}'");
        $menuElemanlari = Menu::model()->with('menuOges')->find($criteria);
        $eklenenAnaElemanlar = array();
        foreach ($menuElemanlari->menuOges as $menuEleman) {
            $menuAltElemanlari = array();
            if($altOgeleriGetir){
               $menuAltElemanlari = Oge::model()->findAllByAttributes(array('ust_oge_id' => $menuEleman->id)); 
            }
            if (!empty($menuAltElemanlari)) {
                $eklenenAltElemanlar = array();
                foreach ($menuAltElemanlari as $menuAltElemani) {
                    $eklenenAltElemanlar = $this->altMenuleriDoldur($eklenenAltElemanlar, $menuAltElemani);
                }
                array_push($eklenenAnaElemanlar, array('label' => $menuEleman->ad,
                    'url' => array($menuEleman->adres),
                    'items' => $eklenenAltElemanlar));
            } else {
                array_push($eklenenAnaElemanlar, array('label' => $menuEleman->ad, 'url' => array($menuEleman->adres)));
            }
        }
        return $eklenenAnaElemanlar;
    }
    
    public function altMenuleriDoldur($eklenenAltElemanlar, $menuAltElemani) {
        $menuAltAltElemanlari = Oge::model()->findAllByAttributes(array('ust_oge_id' => $menuAltElemani->id));
        if (!empty($menuAltAltElemanlari)) {
            $eklenenAltAltElemanlar = array();
            foreach ($menuAltAltElemanlari as $menuAltAltElemani) {
                $eklenenAltAltElemanlar = $this->altMenuleriDoldur($eklenenAltAltElemanlar, $menuAltAltElemani);
            }
            array_push($eklenenAltElemanlar, array('label' => $menuAltElemani->ad,
                'url' => array($menuAltElemani->adres),
                'items' => $eklenenAltAltElemanlar));
        } else {
            array_push($eklenenAltElemanlar, array('label' => $menuAltElemani->ad, 'url' => array($menuAltElemani->adres)));
        }
        return $eklenenAltElemanlar;
    }

}